package com.systop.ltbook.sms.smscfg.service;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.systop.core.dao.hibernate.BaseHibernateDao;
import com.systop.core.service.Definable;
import com.systop.ltbook.sms.smscfg.model.SmsCfg;

/**
 * 短信发送条数配置管理类
 * @author DU
 */
@Service
public class SmsCfgManager implements Definable{

	/** 基础DAO类 */
	private BaseHibernateDao baseHibernateDao;
	
	/**
   * 设置主键值，短信发送条数配置在数据库中仅存储一行
   */
  private final String PK = "smssendsizeid";
  
  /** 
   * 获取指定配置信息
   */
  public SmsCfg get() {
    return baseHibernateDao.get(SmsCfg.class, PK);
  }
  
  /**
   * 保存短信配置信息
   * @param smsCfg
   */
  @Transactional
  public void save(SmsCfg smsCfg) { 
    Assert.notNull(smsCfg);
    // 若不存在，为其设置主键值
    if (StringUtils.isBlank(smsCfg.getId())) {
    	smsCfg.setId(PK);
    }
    // 保存短信配置信息
    if (get() == null) {
      baseHibernateDao.save(smsCfg);
    } else {
      baseHibernateDao.merge(smsCfg);
    }
  }
  
  @Autowired(required = true)
  public void setBaseHibernateDao(BaseHibernateDao baseHibernateDao) {
    this.baseHibernateDao = baseHibernateDao;
  }
  
	@Override
  public boolean isDefined() {
		return get() != null;
  }

}
